package com.aspose.cells;

class zbqe {
   private CellArea a;
   private int b;
   private int[] c;
   private boolean d;
   private boolean e;
   private boolean f;
   private boolean g;
   private int h;
   private int i;
   private Cells j;
   private RowCollection k;
   private int l;
   private String[] m;
   private String n = "Total";
   private String o = "Grand Total";

   zbqe(Cells var1) {
      this.j = var1;
      this.k = var1.getRows();
      this.g = true;
   }

   void a(CellArea var1, int var2, int var3, int[] var4, boolean var5, boolean var6, boolean var7) {
      this.a = var1;
      this.i = var2;
      this.b = var3;
      this.c = var4;
      this.d = var5;
      this.e = var6;
      this.f = var7;
      this.n = zsl.a(var3, this.j.r().n().getSettings());
      this.o = "Grand " + this.n;
      this.l = var1.StartColumn + var2;
      boolean var8 = false;

      int var9;
      for(var9 = var4.length - 1; var9 >= 0; --var9) {
         if (var4[var9] == var2) {
            var8 = true;
         }
      }

      int var10;
      if (var8) {
         var8 = false;

         for(var9 = var2 - 1; var9 >= 0; --var9) {
            var8 = true;

            for(var10 = var4.length - 1; var10 >= 0; --var10) {
               if (var4[var10] == var9) {
                  var8 = false;
                  break;
               }
            }

            if (var8) {
               this.l = var1.StartColumn + var9;
               break;
            }
         }

         if (!var8) {
            this.l = var1.StartColumn;
            this.j.insertColumn(var1.StartColumn);
            ++var1.StartColumn;
            ++var1.EndColumn;
         }
      }

      this.m = new String[var4.length];

      for(var9 = 0; var9 < var4.length; ++var9) {
         this.m[var9] = CellsHelper.columnIndexToName(var4[var9] + var1.StartColumn);
         var4[var9] += var1.StartColumn;
      }

      this.h = var1.StartRow;

      for(this.h = var1.StartRow; this.h <= var1.EndRow; ++this.h) {
         var9 = this.j.c().a(this.h + 1);
         if (var9 != -1) {
            this.j.c().removeAt(var9);
         }
      }

      this.i = var1.StartColumn + var2;
      if (var5) {
         boolean var14 = false;

         int var11;
         for(this.h = var1.StartRow; this.h <= var1.EndRow; ++this.h) {
            Row var13 = this.k.a(this.h, true, false, false);
            if (var13 != null) {
               if (var13.h() != 0) {
                  var13.a((byte)0);
                  var14 = true;
               }

               for(var11 = 0; var11 < var4.length; ++var11) {
                  Cell var12 = var13.getCellOrNull(var4[var11]);
                  if (var12 != null && var12.f() && var12.getFormula().startsWith("=SUBTOTAL(")) {
                     this.j.deleteRow(this.h--);
                     --var1.EndRow;
                     break;
                  }
               }
            }
         }

         if (var14) {
            var10 = 0;

            for(var11 = 0; var11 < this.k.getCount(); ++var11) {
               Row var15 = this.k.getRowByIndex(var11);
               if (var15.getIndex() >= var1.StartRow) {
                  if (var15.getIndex() > var1.EndRow) {
                     break;
                  }

                  if ((var15.h() & 255) > var10) {
                     var10 = var15.h() & 255;
                  }
               }
            }

            this.j.c((byte)var10);
         }
      } else if (this.b(var1.StartRow)) {
         var7 = false;
      }

      if (var7) {
         this.b();
      } else {
         this.c();
      }

   }

   private boolean a(Row var1) {
      for(int var2 = this.a.StartColumn; var2 <= this.a.EndColumn; ++var2) {
         Cell var3 = var1.getCellOrNull(var2);
         if (var3 != null && var3.f() && var3.getFormula().startsWith("=SUBTOTAL(")) {
            return true;
         }
      }

      return false;
   }

   private boolean b(int var1) {
      Row var2 = this.j.getRows().a(var1, true, false, false);
      if (var2 == null) {
         return false;
      } else {
         for(int var3 = this.a.StartColumn; var3 <= this.a.EndColumn; ++var3) {
            Cell var4 = var2.getCellOrNull(var3);
            if (var4 != null && var4.f() && var4.getFormula().startsWith("=SUBTOTAL(")) {
               return true;
            }
         }

         return false;
      }
   }

   private void a() {
      for(this.h = this.a.EndRow; this.h >= this.a.StartRow; --this.h) {
         boolean var1 = this.b(this.h);
         if (var1) {
            this.j.deleteRow(this.h);
            --this.a.EndRow;
         }
      }

   }

   private void b() {
      boolean var1 = false;
      byte var2 = -1;
      String var3 = "";
      int var4 = -1;
      boolean var5 = a(this.b);
      int var6 = zsl.i(this.b);
      String var7 = "=SUBTOTAL(" + var6 + ",";
      Cell var8 = null;
      Style var9 = new Style(this.j.r());
      var9.getFont().setBold(true);
      var9.setTextWrapped(true);
      int var10 = this.j.r().a(var9);
      if (this.d) {
         this.a();
      }

      int var11 = 0;
      int[] var12 = null;

      for(this.h = this.a.StartRow; this.h <= this.a.EndRow; ++this.h) {
         var11 = 0;
         Row var13 = this.k.a(this.h, true, false, false);
         if (var13 != null) {
            boolean var14 = false;
            boolean var15 = this.a(var13);
            String var16 = "";
            if (var15) {
               var14 = true;
            } else {
               Cell var17 = var13.getCellOrNull(this.i);
               if (var17 != null && var17.getType() != 3) {
                  var16 = var17.j();
               }

               if ("".equals(var16)) {
                  if (var4 == -1 || "".equals(var3)) {
                     continue;
                  }

                  var14 = true;
               } else {
                  if (var4 == -1) {
                     if (!"".equals(var16)) {
                        var3 = var16;
                        var4 = this.h;
                     }
                     continue;
                  }

                  if (com.aspose.cells.b.a.zv.a(var16, var3, true) == 0) {
                     var3 = var16;
                     continue;
                  }

                  var14 = true;
               }
            }

            if (var14) {
               int var23;
               if (!"".equals(var3) && var4 != -1) {
                  this.j.insertRow(this.h);
                  ++this.a.EndRow;

                  for(var23 = 0; var23 < this.c.length; ++var23) {
                     StringBuilder var18 = new StringBuilder(var7);
                     var18.append(this.m[var23]);
                     var18.append(var4 + 1);
                     var18.append(':');
                     var18.append(this.m[var23]);
                     var18.append(this.h);
                     var18.append(')');
                     Cell var19 = this.j.a(this.h, this.c[var23], false);
                     var19.setFormula(com.aspose.cells.b.a.zr.a(var18));
                     this.a(var5, var19);
                  }

                  var8 = this.j.a(this.h, this.l, false);
                  var8.putValue(var3 + " " + this.n);
                  var8.b(var10);
                  if (this.g) {
                     if (var2 != -1) {
                        this.j.groupRows(var4, var2 - 1, false);
                     } else if (var4 != -1 && var4 < this.h) {
                        this.j.groupRows(var4, this.h - 1, false);
                     }
                  }

                  var2 = -1;
                  if (this.e && !var1) {
                     this.j.c().add(this.h + 1, 0);
                  }

                  var3 = var16;
                  ++this.h;
               }

               if (var15) {
                  var12 = new int[this.c.length];

                  for(var23 = 0; var23 < this.c.length; ++var23) {
                     Cell var24 = this.j.get(this.h - 1, this.c[var23]);
                     var12[var23] = var24.s();
                  }

                  var11 = 1;
                  ++this.h;

                  while(this.h <= this.a.EndRow) {
                     var13 = this.k.a(this.h, true, false, false);
                     if (var13 == null || !this.a(var13)) {
                        break;
                     }

                     ++var11;
                     ++this.h;
                  }

                  if (this.h > this.a.EndRow) {
                     var1 = true;
                     this.h -= var11 / 2;
                     break;
                  }

                  --this.h;
                  var4 = -1;
               } else if (!"".equals(var16)) {
                  var4 = this.h;
               }
            }
         }
      }

      int var20;
      Cell var21;
      StringBuilder var22;
      if (!var1 && var4 != -1 && !"".equals(var3)) {
         this.j.insertRows(this.h, 1);
         var8 = this.j.a(this.h, this.l, false);
         var8.putValue(var3 + " " + this.n);
         var8.b(var10);
         if (this.g) {
            this.j.groupRows(var4, this.h - 1, false);
         }

         for(var20 = 0; var20 < this.c.length; ++var20) {
            var22 = new StringBuilder(var7);
            var22.append(this.m[var20]);
            var22.append(var4 + 1);
            var22.append(':');
            var22.append(this.m[var20]);
            var22.append(this.h);
            var22.append(')');
            var21 = this.j.a(this.h, this.c[var20], false);
            var21.setFormula(com.aspose.cells.b.a.zr.a(var22));
            this.a(var5, var21);
         }

         ++this.h;
      }

      this.j.insertRows(this.h, 1);
      var8 = this.j.a(this.h, this.l, false);
      var8.putValue(this.o);
      var8.b(var10);

      for(var20 = 0; var20 < this.c.length; ++var20) {
         var22 = new StringBuilder(var7);
         var22.append(this.m[var20]);
         var22.append(this.a.StartRow + 1);
         var22.append(':');
         var22.append(this.m[var20]);
         var22.append(this.h - var11 / 2 - 1);
         var22.append(')');
         var21 = this.j.a(this.h, this.c[var20], false);
         var21.setFormula(com.aspose.cells.b.a.zr.a(var22));
         this.a(var5, var21);
         if (var12 != null) {
            var21.b(var12[var20]);
         }
      }

      if (this.g && !var1) {
         this.j.groupRows(this.a.StartRow, this.h - 1, false);
      }

      this.j.c().add(this.h + 1, 0);
   }

   private void c() {
      boolean var1 = false;
      int var2 = -1;
      String var3 = "";
      int var4 = -1;
      int var5 = zsl.i(this.b);
      String var6 = "=SUBTOTAL(" + var5 + ",";
      Cell var7 = null;
      Style var8 = new Style(this.j.r());
      var8.getFont().setBold(true);
      var8.setTextWrapped(true);
      int var9 = this.j.r().a(var8);

      for(this.h = this.a.EndRow; this.h >= this.a.StartRow; --this.h) {
         Cell var10 = this.j.getRows().a(this.h, this.i, true, false, false);
         if (var10 != null && var10.getType() != 3) {
            String var11 = var10.j();
            if (var4 == -1) {
               var3 = var11;
               var4 = this.h < this.a.EndRow ? this.a.EndRow : this.h;
            } else if (!com.aspose.cells.b.a.zv.b(var11, var3)) {
               if (!this.d) {
                  Row var12 = this.j.getRows().a(this.h, true, false, false);
                  boolean var13 = false;

                  for(int var14 = 0; var14 < this.c.length; ++var14) {
                     var10 = var12.getCellOrNull(this.c[var14]);
                     if (var10 != null && var10.f() && var10.getFormula().startsWith("=SUBTOTAL(")) {
                        var13 = true;
                        break;
                     }
                  }

                  if (var13) {
                     if (var2 == -1) {
                        var2 = this.h;
                     }

                     if (this.f || this.h != this.a.StartRow) {
                        continue;
                     }

                     var1 = true;
                  }
               }

               int var17 = this.h + 1;
               ++var4;
               this.j.insertRow(var17);
               ++this.a.EndRow;

               for(int var18 = 0; var18 < this.c.length; ++var18) {
                  StringBuilder var19 = new StringBuilder(var6);
                  var19.append(this.m[var18]);
                  var19.append(var17 + 2);
                  var19.append(':');
                  var19.append(this.m[var18]);
                  var19.append(var4 + 1);
                  var19.append(')');
                  this.j.a(var17, this.c[var18], false).setFormula(com.aspose.cells.b.a.zr.a(var19));
               }

               var7 = this.j.a(var17, this.l, false);
               var7.putValue(var3 + " " + this.n);
               var7.b(var9);
               if (this.g) {
                  if (var2 != -1) {
                     this.j.groupRows(var2 + 2, var4, false);
                  } else {
                     this.j.groupRows(var17 + 1, var4, false);
                  }
               }

               var2 = -1;
               if (this.e && !var1) {
                  this.j.c().add(var4 + 1, 0);
               }

               var3 = var11;
               var4 = this.h;
               if (var1) {
                  break;
               }
            }
         }
      }

      ++this.h;
      if (!var1) {
         ++var4;
         this.j.insertRows(this.h, 1);
         ++this.a.EndRow;
         var7 = this.j.a(this.h, this.l, false);
         var7.putValue(var3 + " " + this.n);
         var7.b(var9);
         if (this.g) {
            this.j.groupRows(this.h + 1, var4, false);
         }

         int var15;
         StringBuilder var16;
         for(var15 = 0; var15 < this.c.length; ++var15) {
            var16 = new StringBuilder(var6);
            var16.append(this.m[var15]);
            var16.append(this.h + 2);
            var16.append(':');
            var16.append(this.m[var15]);
            var16.append(var4 + 1);
            var16.append(')');
            this.j.a(this.h, this.c[var15], false).setFormula(com.aspose.cells.b.a.zr.a(var16));
         }

         if (this.d) {
            this.j.insertRows(this.h, 1);
            ++this.a.EndRow;
            var7 = this.j.a(this.h, this.l, false);
            var7.putValue(this.o);
            var7.b(var9);

            for(var15 = 0; var15 < this.c.length; ++var15) {
               var16 = new StringBuilder(var6);
               var16.append(this.m[var15]);
               var16.append(this.a.StartRow + 2);
               var16.append(':');
               var16.append(this.m[var15]);
               var16.append(this.a.EndRow + 1);
               var16.append(')');
               this.j.a(this.h, this.c[var15], false).setFormula(com.aspose.cells.b.a.zr.a(var16));
            }
         }
      }

      if (this.g) {
         this.j.groupRows(this.a.StartRow + 1, this.a.EndRow, false);
      }

      this.j.c().add(var4 + 1, 0);
   }

   private void a(boolean var1, Cell var2) {
      if (var1) {
         Style var3 = var2.o();
         var3.setNumber(0);
         var3.setCustom((String)null);
         var2.a(var3);
      }

   }

   static boolean a(int var0) {
      switch(var0) {
      case 1:
      case 5:
      case 6:
      case 7:
      case 8:
      case 9:
      case 10:
         return true;
      case 2:
      case 3:
      case 4:
      default:
         return false;
      }
   }
}
